package io.github.talelin.latticy.service;

import io.github.talelin.autoconfigure.exception.ForbiddenException;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.latticy.common.UserUtil;
import io.github.talelin.latticy.entity.Record;
import io.github.talelin.latticy.repository.RecordRepository;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class RecordService {

    @Autowired
    private RecordRepository recordRepository;

    @Autowired
    private VacationService vacationService;

    @Autowired
    private ExpenseService expenseService;

    @Autowired
    private InvoiceService invoiceService;

    public Integer getDealNum() {
        Long examineUserId = UserUtil.getCurrentUserId();
        return recordRepository.findExamineUserDealNum(examineUserId);
    }

    public Integer getFeedbackNum() {
        Long userId = UserUtil.getCurrentUserId();
        return recordRepository.findUserFeedbackNum(userId);
    }

    public void create(Long userId, Long examineUserId, Long rootUserId, int type, Long typeId) {
        Record record = new Record();
        Date now = new Date();
        record.setUserId(userId);
        record.setExamineUserId(examineUserId);
        record.setRootUserId(rootUserId);
        record.setStatus(1);
        record.setCreateTime(now);
        record.setType(type);
        record.setTypeId(typeId);
        recordRepository.save(record);
    }

    public Page<Record> getUserRecords(int page, int size) {
        Pageable pageable = PageRequest.of(page, size);
        Long userId = UserUtil.getCurrentUserId();
        return recordRepository.findByUserIdOrderByCreateTimeDesc(userId, pageable);
    }

    public Page<Record> getExamineUserRecords(Boolean isDeal, int page, int size) {
        Pageable pageable = PageRequest.of(page, size);
        Long examineUserId = UserUtil.getCurrentUserId();
        List<Integer> status = new ArrayList<>();
        if (isDeal) {
            //已处理
            status.add(2);
            status.add(3);
        } else {
            //待处理
            status.add(1);
        }
        return recordRepository.findByExamineUserIdAndStatusInOrderByCreateTimeDesc(examineUserId, status, pageable);
    }

    public Page<Record> getRootUserRecords(int page, int size) {
        Pageable pageable = PageRequest.of(page, size);
        Long rootUserId = UserUtil.getCurrentUserId();
        return recordRepository.findByRootUserIdOrderByCreateTimeDesc(rootUserId, pageable);
    }

    public Record getRecord(Long id) {
        Record record = recordRepository.findFirstById(id);
        if (record == null) {
            throw new NotFoundException();
        }
        Long userId = UserUtil.getCurrentUserId();
        if (record.getFeedback() == 1 && userId == record.getUserId()) {
            //取消反馈标记
            record.setFeedback(0);
            recordRepository.save(record);
        }
        return record;
    }

    @Transactional
    public void updateRecordStatus(Long id, int status) {
        Record record = this.getRecord(id);
        if (record.getStatus() != 1) {
            throw new ForbiddenException(20004);
        }
        if (status == 1) {
            status = 4; //撤回
        }
        record.setStatus(status);
        record.setFeedback(1);
        recordRepository.save(record);
        //同步请假，报销，发票状态
        switch (record.getType()) {
            //请假
            case 1:
                vacationService.updateStatus(record.getTypeId(), status);
                break;
            //报销
            case 2:
                expenseService.updateStatus(record.getTypeId(), status);
                break;
            //发票
            case 3:
                invoiceService.updateStatus(record.getTypeId(), status);
                break;
        }
    }

    public void updateStatusToWait(int type, Long typeId) {
        Record record = recordRepository.findByTypeAndTypeId(type, typeId);
        if (record == null) {
            throw new NotFoundException();
        }
        record.setStatus(1);
        recordRepository.save(record);
    }

}
